package org.streets.commons.util;

import java.io.File;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;


public class SAXUtils {

	private static final Log log = LogFactory.getLog(SAXUtils.class.getName());

	public static SAXParserFactory parserFactory;

	static {
		if (parserFactory == null) {
			parserFactory = SAXParserFactory.newInstance();
		}
	}

	public static void parse(File file, DefaultHandler handler) throws Exception {
		try {
			SAXParser parser = parserFactory.newSAXParser();
			parser.parse(file, handler);

		} catch (Exception ex) {
			log.error("parse error : "+ex.getMessage());
			throw ex;
		}
	}


	public static void parse(InputSource source, DefaultHandler handler) throws Exception {
		try {
			SAXParser parser = parserFactory.newSAXParser();
			parser.parse(source, handler);

		} catch (Exception ex) {
			log.error("parse error : "+ex.getMessage());
			throw ex;
		}
	}
	//public static

	public static void parse(InputStream stream, DefaultHandler handler) throws Exception {
		try {
			SAXParser parser = parserFactory.newSAXParser();
			parser.parse(stream, handler);
		} catch (Exception ex) {
			log.error("parse error : "+ex.getMessage());
			throw ex;
		}
	}
}
